package com.cy.核心编程.thead.theadPool;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Scheduled: 延迟线程的生命周期schedule
 */
public class TestSchedule {

    private static AtomicInteger i = new AtomicInteger();

    public static void main(String[] args) {
        //schedule必须用ScheduledExecutorService对象
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(4);

        ses.schedule(()->{
            System.out.println("我是5秒钟之后和大家见面");
        },5, TimeUnit.SECONDS);

        ses.scheduleAtFixedRate(()->{
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        },10,1,TimeUnit.SECONDS);

        for (int i = 0; i < 10; i++) {
            System.out.println("主线程" + i);
        }
    }
}
